package com.al.servlet;

import java.io.IOException;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.al.repository.DBHelper;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private DBHelper helper = new DBHelper();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		switch(method) {
		case "login":
			ResultSet rs = null;
			String password2 = "";
			String name = req.getParameter("userName");
			String password = req.getParameter("passwd");
			String sql = "select passwd from student where userName='"+name+"'";
			try {
				rs = helper.executeQuery(sql);
				while(rs.next()) {
					password2 = rs.getString(1);
				}
				System.out.println(password2);
				if(password.equals(password2)) {
					//登录成功
					req.getRequestDispatcher("index.jsp").forward(req, resp);
				}else {
					resp.sendRedirect("login.jsp");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			break;
		}
		try {
			helper.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
